package cn.sccdlg.crawler.job.service.impl;

import cn.sccdlg.crawler.job.dao.JobInfoRepository;
import cn.sccdlg.crawler.job.pojo.JobInfo;
import cn.sccdlg.crawler.job.service.JobInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author RaoShuang
 * @emil 991207823@qq.com
 * @date 2020/11/30 21:18
 * 招聘信息业务层实现
 */
@Service
public class JobInfoServiceImpl implements JobInfoService {

    @Autowired
    private JobInfoRepository jobInfoRepository;

    @Override
    @Transactional
    public void save(JobInfo jobInfo) {
        //根据url和发布时间查询数据
        JobInfo param = new JobInfo();
        param.setUrl(jobInfo.getUrl());
        param.setTime(jobInfo.getTime());

        //执行查询
        List<JobInfo> list = this.findJobInfo(param);
        //判断查询结果是否为空，为空表示招聘数据不存在，或者已经更新了，需要新增或更新
        if (list == null || list.size() == 0) {
            this.jobInfoRepository.saveAndFlush(jobInfo);
        }

    }

    @Override
    public List<JobInfo> findJobInfo(JobInfo jobInfo) {
        // 设置查询条件
        Example example = Example.of(jobInfo);
        // 执行条件获取查询结果
        List list = this.jobInfoRepository.findAll(example);
        return list;
    }

    @Override
    public Page<JobInfo> findAllPage(int count, int i) {
        Page<JobInfo> all = this.jobInfoRepository.findAll(PageRequest.of(count - 1, i));
        return all;
    }

}
